package com.LostAndFound.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.LostAndFound.domain.User;
import com.LostAndFound.service.UserRegisterService;
import com.LostAndFound.utils.CommonsUtils;

public class UserRegisterServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	UserRegisterService service = new UserRegisterService();
	// 验证用户名是否可用
	public void checkUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		boolean isUsed = false;
		isUsed = service.checkUsername(username);
		if(isUsed) {
			response.getWriter().write("false");
		}else {
			response.getWriter().write("true");
		}
	}
	// 注册用户
	public void regeisterUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String cellphone = request.getParameter("cellphone");
		Integer gender = Integer.parseInt(request.getParameter("gender"));
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		// 封装数据
		User user = new User();
		user.setUid(CommonsUtils.getUUID());
		user.setUsername(username);
		user.setPassword(password);
		user.setRealname(realname);
		user.setCellphone(cellphone);
		user.setGender(gender);
		user.setEmail(email);
		user.setResume(resume);
		// 传递到 service 层
		boolean success = service.regeisterUser(user);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}